package com.ziki.test.socket.thread;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ziki.test.socket.MySocket;
import com.ziki.test.socket.ServerMain;
import com.ziki.test.socket.protocol.SMPP;

public class ListenThread extends Thread {
    ServerSocket                                                serverSocket;
    @Deprecated
    public static Map<Integer, Boolean> mpSendCmd           = new HashMap<Integer, Boolean>();
    @Deprecated
    public static Map<Integer, Boolean> mpRecvCmd           = new HashMap<Integer, Boolean>();

    public static List<Integer>                 lstPrintSend    = new ArrayList<Integer>();
    public static List<Integer>                 lstPrintRecv    = new ArrayList<Integer>();

    /**
     * 监听端口，提供查询网关状态及将内存队列存入文件的Socket服务
     */
    public ListenThread(int intPort) {
        if (createServerSocket(intPort)) {
            initSendType();
            // System.out.println(isPrintLog(1));
            // System.out.println(isPrintLog(2));
            // System.out.println(isPrintLog(0x00000015));
            // System.out.println(isPrintRecvLog(1));
            // System.out.println(isPrintRecvLog(2));
            // System.out.println(isPrintRecvLog(0x00000015));
            // System.out.println("lstPrintSend.size()" + lstPrintSend.size());
            // System.out.println("lstPrintRecv.size()" + lstPrintRecv.size());
            this.start();
        }
    }

    /**
     * 创建服务端ServerSocket
     * 
     * @return
     */
    public boolean createServerSocket(int intPort) {
        try {
            serverSocket = new ServerSocket(intPort);
            return true;
        }
        catch (IOException ex) {
            return false;
        }
    }

    public void run() {
        while (true) {
            Socket socket = null;
            try {
                socket = serverSocket.accept(); // 接收socket
                if (socket != null) {
                    System.out.println("accept socket==========");
                    MySocket mySocket = new MySocket(socket, 0, 0, 0);
                    mySocket.startRecv();
                    mySocket.startSend();
                }
            }
            catch (IOException ioe) {
            }
            finally {
                this.threadSleep(100);
            }
        }
    }

    public void initSendType() {
        SMPP smpp = new SMPP();
        mpSendCmd.clear();
        mpRecvCmd.clear();
        lstPrintRecv.clear();
        lstPrintSend.clear();
        String[] strCommond = ServerMain.strRecevType.split(",");
        for (int i = 0; i < strCommond.length; i++) {
            // System.out.println(strCommond[i]);
            if ("link".equals(strCommond[i])) {
                mpRecvCmd.put(smpp.get_ACTIVE_TEST(), true);
                lstPrintRecv.add(smpp.get_ACTIVE_TEST());
            }
            else if ("linkresp".equals(strCommond[i])) {
                mpSendCmd.put(smpp.get_ACTIVE_TEST(), true);
                lstPrintSend.add(smpp.get_ACTIVE_TEST());
            }
            else if ("deliver".equals(strCommond[i])) {
                lstPrintSend.add(smpp.get_DELIVER());
                mpSendCmd.put(smpp.get_DELIVER(), true);
            }
            else if ("deliverresp".equals(strCommond[i])) {
                mpRecvCmd.put(smpp.get_DELIVER_RESP(), true);
                lstPrintRecv.add(smpp.get_DELIVER_RESP());
            }
            else if ("exit".equals(strCommond[i])) {
                mpSendCmd.put(smpp.get_EXIT(), true);
            }
            else if ("exitresp".equals(strCommond[i])) {
                mpRecvCmd.put(smpp.get_EXIT_RESP(), true);
            }
            else if ("login".equals(strCommond[i])) {
                mpRecvCmd.put(smpp.get_LOGIN(), true);
                lstPrintRecv.add(smpp.get_LOGIN());
            }
            else if ("login2".equals(strCommond[i])) {
                mpRecvCmd.put(smpp.get_LOGIN2(), true);
                lstPrintRecv.add(smpp.get_LOGIN2());
            }
            else if ("login3".equals(strCommond[i])) {
                mpRecvCmd.put(smpp.get_LOGIN3(), true);
                lstPrintRecv.add(smpp.get_LOGIN3());
            }
            else if ("login2resp".equals(strCommond[i])) {
                mpSendCmd.put(smpp.get_LOGIN2(), true);
                lstPrintSend.add(smpp.get_LOGIN2());
            }
            else if ("loginresp".equals(strCommond[i])) {
                mpSendCmd.put(smpp.get_LOGIN(), true);
                lstPrintSend.add(smpp.get_LOGIN());
            }
            else if ("login3resp".equals(strCommond[i])) {
                mpSendCmd.put(smpp.get_DELIVER(), true);
                lstPrintSend.add(smpp.get_DELIVER());
            }
            else if ("submit".equals(strCommond[i])) {
                mpRecvCmd.put(smpp.get_SUBMIT(), true);
                lstPrintRecv.add(smpp.get_SUBMIT());
            }
            else if ("submitresp".equals(strCommond[i])) {
                mpSendCmd.put(smpp.get_SUBMIT(), true);
                lstPrintSend.add(smpp.get_SUBMIT());
            }
        }
    }

    public static boolean isPrintLog(int commandID) {
        // System.out.println("commandID:" + commandID);
        // System.out.println("ListenThread.lstPrintSend.isEmpty()" +
        // ListenThread.lstPrintSend.isEmpty());
        // System.out.println("ListenThread.lstPrintSend.contains(commandID)" +
        // ListenThread.lstPrintSend.contains(commandID));
        return ListenThread.lstPrintSend.isEmpty() ? true : ListenThread.lstPrintSend.contains(commandID);
    }

    public static boolean isPrintRecvLog(int commandID) {
        // System.out.println("commandID:" + commandID);
        // System.out.println("ListenThread.lstPrintRecv.isEmpty()" +
        // ListenThread.lstPrintRecv.isEmpty());
        // System.out.println("ListenThread.lstPrintRecv.contains(commandID)" +
        // ListenThread.lstPrintRecv.contains(commandID));
        return ListenThread.lstPrintRecv.isEmpty() ? true : ListenThread.lstPrintRecv.contains(commandID);
    }

    public void threadSleep(int miniSec) {
        try {
            this.sleep(miniSec);
        }
        catch (InterruptedException e) {
        }
    }
}
